package com.nine.pesys.util.common;

import com.nine.pesys.model.entity.Equipment;
import com.nine.pesys.model.entity.EquipmentDetail;
import com.nine.pesys.model.entity.SportField;
import com.nine.pesys.model.entity.SportFieldDetail;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by L先森 on 2015/1/2.
 */
public class OGNLStaticMethod {
    public static Integer getRowspan(List<EquipmentDetail> equipmentDetails,Equipment equipment) {
//        Integer[] sum = {};
//        int index = 0;
//        for(int i=0;i<equipmentDetails.size();i++){
//            EquipmentDetail equipmentDetail = equipmentDetails.get(i);
//            if(i>=1){
//                if (equipmentDetail.getEquipment().getName().equals(equipmentDetails.get(i-1).getEquipment().getName())){
//                    sum[index]++;
//                }else {
//                    index++;
//                    sum[index]++;
//                }
//            }
//
//        }
//        System.out.println(sum[0]+"=============================");
//        return sum;
        Integer rows = 0;
        for (EquipmentDetail equipmentDetail : equipmentDetails) {
            if (equipmentDetail.getEquipment().getName().equals(equipment.getName())) {
                rows++;
            }
        }
        return rows;
    }

    public static Integer getTimes(List<EquipmentDetail> equipmentDetails) {
        Integer sum = 0;
        for (int i = 0; i < equipmentDetails.size(); i++) {
            EquipmentDetail equipmentDetail = equipmentDetails.get(i);
            if (i >= 1) {
                if (!equipmentDetail.getEquipment().getName().equals(equipmentDetails.get(i - 1).getEquipment().getName())) {
                    sum++;
                }
            }
        }
        return sum;
    }

    public static Integer getTimes_SF(List<SportFieldDetail> sportFieldDetails,int index){
        Integer row = 0;
        Integer strIndex = 0;
        Integer rows[] = new Integer[sportFieldDetails.size()-1];
        String[] str = new String[sportFieldDetails.size()-1];
//        Integer rows[] = new Integer[100];
        String sportName = "";
        if (sportFieldDetails != null && sportFieldDetails.size() > 0) {
            sportName = sportFieldDetails.get(0).getSportField().getName();
            str[strIndex] = sportName;
        }
        if (sportName != null && !sportName.equals("")) {
            for (int i = 0; i <sportFieldDetails.size()-1; i++) {
                for (SportFieldDetail sportFieldDetail : sportFieldDetails) {
                    if (sportName.equals(sportFieldDetail.getSportField().getName())) {
                        row++;
                    }
                    if (!sportName.equals(sportFieldDetail.getSportField().getName())&&i<=0){
                        strIndex++;
                        str[strIndex] = sportFieldDetail.getSportField().getName();
                    }
                }
//                将i=0留个indexs[0]
                rows[i] = row;
//                System.out.println(rows[i]+"============");
                row = 0;
//                System.out.println(strIndex);
                if (i < strIndex) {
                    sportName = str[i+1];
                }
            }
            for (int i = 0 ; i <6;i++){
            }

        }
//        System.out.println(rows[0]);
        for (int i = 0 ;i <sportFieldDetails.size();i++){
        }
        Integer[] indexs  = new Integer[rows.length+1];
        indexs[0] = 0;
//        System.out.println(rows.length);
        for (int i = 0;i<rows.length;i++){
//            System.out.println(rows[i]);
            indexs[i+1] = indexs[i]+rows[i];
        }
        for (int i = 0 ;i<indexs.length;i++){
            if (indexs[i]==index){
                System.out.println(indexs[i]);
                return index;
            }
        }
        return null;

    }

    public static Integer getRowspan_SF(List<SportFieldDetail> sportFieldDetails,int index) {
//        Integer row = 0;
//        Integer strIndex = 0;
//        String[] str = new String[sportFieldDetails.size()-1];
//        Integer rows[] = new Integer[sportFieldDetails.size()];
//        String sportName = "";
//        if (sportFieldDetails != null && sportFieldDetails.size() > 0) {
//            sportName = sportFieldDetails.get(0).getSportField().getName();
//            str[strIndex] = sportName;
//        }
//        if (sportName != null && !sportName.equals("")) {
//            for (int i = 0; i < sportFieldDetails.size()-1; i++) {
//                for (SportFieldDetail sportFieldDetail : sportFieldDetails) {
//                    if (sportName.equals(sportFieldDetail.getSportField().getName())) {
//                        row++;
//                    }
//                    if (!sportName.equals(sportFieldDetail.getSportField().getName())&&i<=0){
//                        strIndex++;
//                        str[strIndex] = sportFieldDetail.getSportField().getName();
//                    }
//                }
//                rows[i] = row;
//                row = 0;
//                if (i < strIndex) {
//                    System.out.println(str[i]+"===========================");
//                    sportName = str[i+1];
//                }
//            }
//
//        }
////        System.out.println(rows[0]);
//        System.out.println(index+"==========================");
//        for (int i = 0 ;i <rows.length;i++){
//            System.out.println(rows[i]+"=============row[i]=================");
//        }
//        return rows[index+1];
        Map<Integer,Integer> idMap = new HashMap<Integer, Integer>();
        Integer row = 0;
        Integer strIndex = 0;
        Integer rows[] = new Integer[sportFieldDetails.size()-1];
        String[] str = new String[sportFieldDetails.size()-1];
//        Integer rows[] = new Integer[100];
        String sportName = "";
        if (sportFieldDetails != null && sportFieldDetails.size() > 0) {
            sportName = sportFieldDetails.get(0).getSportField().getName();
            str[strIndex] = sportName;
        }
        if (sportName != null && !sportName.equals("")) {
            for (int i = 0; i <sportFieldDetails.size()-1; i++) {
                for (SportFieldDetail sportFieldDetail : sportFieldDetails) {
                    if (sportName.equals(sportFieldDetail.getSportField().getName())) {
                        row++;
                    }
                    if (!sportName.equals(sportFieldDetail.getSportField().getName())&&i<=0){
                        strIndex++;
                        str[strIndex] = sportFieldDetail.getSportField().getName();
                    }
                }
//                将i=0留个indexs[0]
                rows[i] = row;
//                System.out.println(rows[i]+"============");
                row = 0;
//                System.out.println(strIndex);
                if (i < strIndex) {
                    sportName = str[i+1];
                }
            }
            for (int i = 0 ; i <6;i++){
            }

        }
//        System.out.println(rows[0]);
        for (int i = 0 ;i <sportFieldDetails.size();i++){
        }
        Integer[] indexs  = new Integer[rows.length+1];
        indexs[0] = 0;
//        System.out.println(rows.length);
        for (int i = 0;i<rows.length;i++){
//            System.out.println(rows[i]);
            indexs[i+1] = indexs[i]+rows[i];
        }
//        for (int i = 0 ;i<indexs.length;i++){
//            if (indexs[i]==index){
//                System.out.println(indexs[i]);
//                return index;
//            }
//        }
        for (int i = 0;i<rows.length;i++){
            idMap.put(indexs[i],rows[i]);
        }
        for (Integer key:idMap.keySet()){
            if (key==index){
                return idMap.get(key);
            }
        }
        return null;

    }


}
